#import "ALHUDTextFieldCell.h"


#pragma mark Constants
static const CGFloat ALHUDTextFieldCornerRadius = 3.0f;
static const CGFloat ALHUDTextFieldHorizontalPadding = 2.0f;


#pragma mark -
#pragma mark Class Globals
static NSColor *ALHUDTextFieldBorderColor;
static NSGradient *ALHUDTextFieldBackgroundGradient;
static NSGradient *ALHUDTextFieldBackgroundGradientHighlighted;


#pragma mark -
@interface ALHUDTextFieldCell (ALHUDTextFieldCellPrivateMethods)
-(NSBezierPath *)framePathWithRect_:(NSRect)frameRect;
@end


#pragma mark -
@implementation ALHUDTextFieldCell

+(void)initialize {
	ALHUDTextFieldBorderColor = [[NSColor colorWithCalibratedWhite:0.75f alpha:1.0f] retain];
	NSColor *topColor = [NSColor colorWithCalibratedWhite:0.55 alpha:1.0f];
	NSColor *bottomColor = [NSColor colorWithCalibratedWhite:0.4 alpha:1.0f];
	ALHUDTextFieldBackgroundGradient = [[NSGradient alloc] initWithStartingColor:topColor endingColor:bottomColor];
	NSColor *highlightedTop = [NSColor colorWithCalibratedWhite:0.45 alpha:1.0];
	NSColor *highlightedBottom = [NSColor colorWithCalibratedWhite:0.35 alpha:1.0];
	ALHUDTextFieldBackgroundGradientHighlighted = [[NSGradient alloc] initWithStartingColor:highlightedTop endingColor:highlightedBottom];
}

-(BOOL)drawsBackground {
	return NO;
}

-(NSBackgroundStyle)backgroundStyle {
	return NSBackgroundStyleDark;
}

-(NSBackgroundStyle)interiorBackgroundStyle {
	return NSBackgroundStyleDark;
}

-(void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
	NSRect borderRect = NSInsetRect(cellFrame, 0.5f, 0.5f);
	NSBezierPath *borderPath = [self framePathWithRect_:borderRect];
	NSGradient *backgroundGradient = [self showsFirstResponder] ? ALHUDTextFieldBackgroundGradientHighlighted : ALHUDTextFieldBackgroundGradient;
	[backgroundGradient drawInBezierPath:borderPath angle:90.0f];
	[ALHUDTextFieldBorderColor setStroke];
	[borderPath stroke];
	
	if ([self showsFirstResponder] && [self focusRingType] != NSFocusRingTypeNone) {
		NSBezierPath *focusPath = [self framePathWithRect_:cellFrame];
		[NSGraphicsContext saveGraphicsState];
		NSSetFocusRingStyle(NSFocusRingOnly);
		[focusPath fill];
		[NSGraphicsContext restoreGraphicsState];
	}
	
	NSRect interiorRect = NSInsetRect(cellFrame, ALHUDTextFieldHorizontalPadding, 0.0f);
	[self drawInteriorWithFrame:interiorRect inView:controlView];
}

-(void)editWithFrame:(NSRect)cellFrame inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent *)theEvent {
	NSRect paddedFrame = NSInsetRect(cellFrame, ALHUDTextFieldHorizontalPadding, 0.0f);
	[super editWithFrame:paddedFrame inView:controlView editor:textObj delegate:anObject event:theEvent];
}

-(void)selectWithFrame:(NSRect)cellFrame inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject start:(NSInteger)selStart length:(NSInteger)selLength {
	NSRect paddedFrame = NSInsetRect(cellFrame, ALHUDTextFieldHorizontalPadding, 0.0f);
	[super selectWithFrame:paddedFrame inView:controlView editor:textObj delegate:anObject start:selStart length:selLength];
}

-(NSBezierPath *)framePathWithRect_:(NSRect)frameRect {
	return [NSBezierPath bezierPathWithRoundedRect:frameRect xRadius:ALHUDTextFieldCornerRadius yRadius:ALHUDTextFieldCornerRadius];
}

@end
